#include "extractorMultiScaleDenseSiftVector.h"

using namespace std;
using namespace cv;

extractorMultiScaleDenseSiftVector::extractorMultiScaleDenseSiftVector(cv::Mat src_img, char* _labelImage, Slic _clustering, Mat dictionary){
	this->size = _clustering.getCenter_counts().size();
	this->labelImage = _labelImage;
	int _nHist = dictionary.rows;
	
	for(int icluster = 0; icluster < size; icluster++){
		multiScaleDenseSiftVector _vector(_nHist,icluster,_labelImage);

		// Get Dense Keypoint  on Cluster
		Mat t_mask = _clustering.creatingMaskForSuperpixel(icluster);
		DenseFeatureDetector detector(1.f,1,0.1f,6,0,true,false);
		vector<KeyPoint> queryKeypoints;
		detector.detect(src_img, queryKeypoints,t_mask);

		_vector.setNumberKeyPoint(queryKeypoints.size());
	
		vector<double> _v = vectorQuatizationHistogram(src_img,queryKeypoints,dictionary);
		
		_vector.setValue(_v);

		this->listVector.push_back(_vector);
	}
}

vector<double> extractorMultiScaleDenseSiftVector::vectorQuatizationHistogram(Mat data_img , std::vector<KeyPoint>keypoints, Mat dictionary){
	Ptr<DescriptorExtractor> sift_extractor = DescriptorExtractor::create("SIFT");
	Ptr<DescriptorMatcher> flan_matcher = DescriptorMatcher::create("FlannBased");
	
	cv::BOWImgDescriptorExtractor bowDE(sift_extractor,flan_matcher);
	
	bowDE.setVocabulary(dictionary);

	cv::Mat bowDescriptor;
	
	try
	{
		bowDE.compute(data_img,keypoints,bowDescriptor);
	}
	catch(exception &e){
		cout << e.what() << endl;
	}
	
	//cv::Mat hist; 
	//hist.push_back(bowDescriptor);

	vector<double> _result = bowDescriptor.row(0);

	//cout << bowDescriptor.rows << endl; // 1
	//cout << bowDescriptor.cols << endl; // 800

	return _result;
}

vector<multiScaleDenseSiftVector> extractorMultiScaleDenseSiftVector::getListVector(){
	return this->listVector;
}